package com.twu.biblioteca;

import com.google.common.collect.ImmutableMap;

import java.util.Map;
import java.util.Scanner;

import static com.google.common.collect.ImmutableMap.of;

public class LoginHandler extends UserManagement implements Handler {
    protected Map<String, String> registeredUsers;

    public LoginHandler() {
        super();
        registeredUsers = of("111-1111", "password");
    }
    @Override
    public Object handle() {
        LibraryUser loginUser = new LibraryUser(getUserInput("please input your username:"), getUserInput("please input your password:"), "Jane", "Jane@thoughtworks.com", "123456");
        if (isRegistered(loginUser)) {
            loginUser.setToggle();
            setCurrentUser(loginUser);
            return "login success";
        } else {
            return "login failed";
        }
    }

    private boolean isRegistered(LibraryUser loginUser) {
        String userName = loginUser.getUserName();
        String userPassword = loginUser.getUserPassword();
        return registeredUsers.containsKey(userName) && userPassword.equals(registeredUsers.get(userName));
    }

    private String getUserInput(String inputPrompt) {
        System.out.print(inputPrompt);
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }

}
